<template>
	<view>
		<uni-list>
			<!-- 传递当前用户身份 -->
			<uni-list-item v-for="(dep,index) in departList" :key="index" :title="dep.depName" link="navigateTo"
				clickable @click="toDepartDetail(dep.depId,dep.depName)" />
		</uni-list>
		<uni-popup ref="depart" type="dialog">
			<uni-popup-dialog title="部门名称" mode="input" @confirm="addDepart"></uni-popup-dialog>
		</uni-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				orgId: null,
				myJob: null,
				departList: []
			}
		},
		onLoad: function(option) { //option为object类型，会序列化上个页面传递的参数
			this.orgId = option.orgId;
			this.myJob = option.myJob;
			this.getDepartList();
		},
		// 右上角更多
		onNavigationBarButtonTap: function(e) {
			if (this.myJob !== '3') {
				uni.showModal({
					content: '权限不足',
					showCancel: false,
					success: res => {
						return
					}
				})
				return
			}
			this.$refs.depart.open();
		},
		onShow() {
			this.getDepartList();
		},
		methods: {
			// 添加部门
			addDepart(done, value) {
				uni.showModal({
					content: `您确定要添加【${value}】部门吗?`,
					success: res => {
						if (res.confirm) {
							this.$http.request({
								url: `/organizations/web/department/createDepartment`,
								method: 'POST',
								data: {
									orgId: this.orgId,
									depName: value
								}
							}).then(res => {
								res=res.data;
								if (res.result) {
									uni.showModal({
										content: res.msg,
										showCancel: false,
										success: res => {
											this.$refs.depart.close();
											this.getDepartList()
										}
									});
								} else {
									uni.showModal({
										content: res.msg,
										showCancel: false
									});
								}
							})
						}
					}
				})
			},
			toDepartDetail(depId, depName) {
				uni.navigateTo({
					url: `./departDetail?orgId=${this.orgId}&depId=${depId}&depName=${depName}&myJob=${this.myJob}`
				})
			},
			getDepartList() {
				this.$http.request({
					url: `/organizations/web/department/depart/${this.orgId}`
				}).then(res => {
					res = res.data;
					if (res.result) {
						this.departList = res.data;
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						});
					}

				})
			}
		}
	}
</script>

<style>
</style>
